import { _decorator, Component, Collider2D, IPhysics2DContact } from "cc";
import { PowerUpType } from "../enums/GameEnums";
const { ccclass, property } = _decorator;

// 道具基类装饰器
export function powerUpConfig(config: {
  duration?: number;
  effectType: PowerUpType;
}) {
  return function (constructor: Function) {
    constructor.prototype.duration = config.duration || 0;
    constructor.prototype.effectType = config.effectType;
  };
}

@ccclass("BasePowerUp")
export class BasePowerUp extends Component {
  protected duration: number = 0;
  protected effectType: PowerUpType = PowerUpType.SINGLE_SHOT;

  onLoad() {
    this.initCollider();
  }

  protected initCollider() {
    const collider = this.getComponent(Collider2D);
    if (collider) {
      collider.on(
        Collider2D.EventType.BEGIN_CONTACT,
        this.onBeginContact,
        this
      );
    }
  }

  protected onBeginContact(
    selfCollider: Collider2D,
    otherCollider: Collider2D,
    contact: IPhysics2DContact | null
  ) {
    // 基类中的碰撞处理逻辑
    this.onPowerUpCollected();
  }

  protected onPowerUpCollected() {
    // 道具被收集时的基础行为
    this.applyEffect();
    this.node.destroy();
  }

  protected applyEffect() {
    // 子类需要重写此方法来实现具体的效果
  }

  public getDuration(): number {
    return this.duration;
  }

  public getEffectType(): PowerUpType {
    return this.effectType;
  }
}
